package com.ocn.li.scmdy.modules.app.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ocn.li.scmdy.common.utils.R;
import com.ocn.li.scmdy.modules.app.entity.*;
import com.ocn.li.scmdy.modules.app.service.DyUserService;
import com.ocn.li.scmdy.modules.app.service.ZjkService;
import com.ocn.li.scmdy.modules.app.utils.ImportExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @ClassName ApiExcelDemoController
 * @Description TODO
 * @Author Li9527
 * @Date 2020/9/27 10:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/excel/demo")
@Api(value = "ApiExcelDemoController", tags = {"移动端--Excel导入导出demo"})
public class ApiExcelDemoController {

    @Autowired
    DyUserService dyUserService;

    @Autowired
    ZjkService zjkService;

    /**
     * @return com.ocn.li.scmdy.common.utils.R
     * @Author Li9527
     * @Description Excel导入demo
     * @Date 2020-9-18 8:55
     * @Param [file]
     **/
    @PostMapping("/importExcel")
    @ApiOperation("Excel导入demo")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            String[] excelHead = {"所属网格", "房屋编号", "房屋地址", "建筑用途", "建筑面积（平方米）",
                    "房主姓名", "证件号码", "房主联系方式", "房主现居详址", "是否为出租房"};
            // 构造对象属性数组
            Field[] fields = ClassUtil.getDeclaredFields(LiTestExcel.class);
            String[] excelHeadAlias = new String[fields.length];
            for (int i = 0; i < fields.length; i++) {
                excelHeadAlias[i] = fields[i].getName();
            }
            List<LiTestExcel> list = ImportExcelUtils.importExcelToBean(
                    file.getInputStream(), excelHead, excelHeadAlias, LiTestExcel.class);
            // todo 接下来就是校验实体字段和持久化，这里不做讲解，但是建议使用insertBatch(),和统一工具类校验字段值规范

            return R.ok("导入成功").put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("导入失败");
        }
    }


    /**
     * @return com.ocn.li.scmdy.common.utils.R
     * @Author Li9527
     * @Description 专家库导入
     * @Date 2020-9-18 8:55
     * @Param [file]
     **/
    @PostMapping("/importExcelZjk")
    @ApiOperation("专家库导入")
    public R importExcelZjk(@RequestParam("file") MultipartFile file) {
        try {
            String[] excelHead = {"所属领域", "姓名", "现工作单位", "专业类别", "职称资格或职务",
                    "联系方式", "专长"};
            // 构造对象属性数组
            Field[] fields = ClassUtil.getDeclaredFields(ZjkExcel.class);
            String[] excelHeadAlias = new String[fields.length];
            for (int i = 0; i < fields.length; i++) {
                excelHeadAlias[i] = fields[i].getName();
            }
            List<ZjkExcel> list = ImportExcelUtils.importExcelToBean(
                    file.getInputStream(), excelHead, excelHeadAlias, ZjkExcel.class);
            // todo 接下来就是校验实体字段和持久化，这里不做讲解，但是建议使用insertBatch(),和统一工具类校验字段值规范
            for (int i = 0; i < list.size(); i++){
                Zjk zjk = new Zjk();
                BeanUtils.copyProperties(list.get(i),zjk);
                zjkService.insert(zjk);
            }
            return R.ok("导入成功").put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("导入失败");
        }
    }

    /**
     * @return com.ocn.li.scmdy.common.utils.R
     * @Author Li9527
     * @Description Excel导出demo
     * @Date 2020-9-27 17:47:29
     * @Param
     **/
    @PostMapping("/exportExcel")
    @ApiOperation("Excel导出demo")
    public void exportExcel(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException {
        dyUserService.exportExcel(params, response);
    }


}
